﻿using NHibernate;

namespace NMA.Infrastructure.NHibernate.UnitOfWork
{
    using NMA.Infrastructure.UnitOfWork;   

    public class UnitOfWork : IUnitOfWork
    {
        private readonly ISessionFactory sessionFactory;        
        private readonly ITransaction transaction;

        public UnitOfWork(ISessionFactory sessionFactory)
        {
            this.sessionFactory = sessionFactory;
            Session = this.sessionFactory.OpenSession();
            transaction = Session.BeginTransaction();
        }

        public ISession Session
        {
            get;
            private set;
        }

        public void Dispose()
        {
            Session.Close();
            Session = null;
        }

        //public void Rollback()
        //{
        //    if (transaction.IsActive)
        //        transaction.Rollback();
        //}

        public void Commit()
        {
            //if (transaction.IsActive)
                transaction.Commit();
        }
    }
}